﻿create PROCEDURE Sync.SP_wfStepInstances_SelectChanges
@LastRowVersion rowversion,
@FilterdColumn UNIQUEIDENTIFIER
AS 
BEGIN 
	SELECT 
        t.[StepInstanceID],
		g.[CreationDate],g.[ProcessInstanceID],g.[StepID],g.[ActionTypeID],g.[PreviousStepInstanceID],g.[CompletionDate],g.[UserID],g.[Comments],
		t.IsDeleted,
		t.LastUpdatedDate
	FROM [dbo].[wfStepInstances] g RIGHT JOIN Sync.wfStepInstances_Tracking t  
    ON t.StepInstanceID = g.StepInstanceID 
	WHERE (@LastRowVersion IS NULL OR t.Version > @LastRowVersion)
	AND ((@FilterdColumn IS NULL) OR
		(t.[StepInstanceID] in 
		(SELECT     wfStepInstances.StepInstanceID
		FROM        wfStepInstances INNER JOIN
					wfProcessInstances ON wfStepInstances.ProcessInstanceID = wfProcessInstances.ProcessInstanceID INNER JOIN
                    CustomerLoans ON wfProcessInstances.EntityID = CustomerLoans.LoanID
		WHERE     (CustomerLoans.CommitteeID = @FilterdColumn))
		OR t.[StepInstanceID] in
		(SELECT     wfStepInstances.StepInstanceID
		FROM         wfStepInstances INNER JOIN
                      wfProcessInstances ON wfStepInstances.ProcessInstanceID = wfProcessInstances.ProcessInstanceID INNER JOIN
                      Board ON wfProcessInstances.EntityID = Board.BoardID
		WHERE     (Board.CommitteeID = @FilterdColumn))))
    END











